import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("WillPopScope"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int lastClickTime = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //ModalRoute
    return WillPopScope(
      onWillPop: () async {
        // 返回值被自动封装成Feature
        int now = DateTime.now().millisecondsSinceEpoch;
        print("are you sure to exit. $now");
        if (now - lastClickTime < 2000) {
          print("可以退出了");
          return true;
        }
        print("再次点击退出");
        lastClickTime = now;
        return false;
      },
      child: Stack(
        children: <Widget>[
          Container(),
        ],
      ),
    );
  }
}
